home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
C++ Spline Class V 1.02
/
C++ Spline Class 1.02.sea
/
Spline Class
/
2DSpline.test.cp
< prev
next >
Wrap
Text File
|
1993-10-01
|
20KB
|
603 lines
/************************************************************************************************/
/* Chris Marshall */
/* */
/* Nikon Electronic Imaging Dept */
/* 1300 Walt Whitman Road */
/* Melville, NY 11747 */
/* (516) 547-4200 */
/* */
/************************************************************************************************/
/* File Name: 2DSpline.test.cp */
/* Description: Test application file for a 2-dimensional B-spline class */
/* Primary Author: Chris Marshall */
/* Version: 1.02 */
/* Project: Library file */
/* Compiler(s): Symantec C++ 6.0 */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/* 10/01/93 Chris Marshall I added a little snippet of code to copy the spline points */
/* when switching from fixed point to floating point modes. */
/* */
/************************************************************************************************/
#include "2DSpline.h"
#define MaxVal(a,b) ((a > b) ? a : b)
#define MinVal(a,b) ((a < b) ? a : b)
Spline *gMySpline;
long *gOldLine;
Point *gOldPoints;
short gOldSelectedPoint;
Boolean gLineDrawn;
#define kDrawSelectedPoint 0
#define kRedrawPoint 1
#define kDrawNew 2
#define kLeftArrowKey 0x1C
#define kRightArrowKey 0x1D
#define kUpArrowKey 0x1E
#define kDownArrowKey 0x1F
#define kTabKey 0x09
#define kDeleteKey 0x08
#define kHitRadius 4
void DisplayWindowCentered ( GrafPtr theWindow );
void CenterWindow ( GrafPtr theWindow );
void DrawControlPoint ( Rect box, short x, short y, Boolean selected );
void DrawSplineLine ( Rect box, short selector );
pascal Boolean FilterProc ( DialogPtr theDialog, EventRecord *theEvent, short *theItem );
/************************************************************************************************/
/* Function Name: CenterWindow */
/* Description: Centers a window on the screen */
/* Primary Author: Chris Marshall */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/************************************************************************************************/
/*##############################################################################################*/
/************************************************************************************************/
void CenterWindow ( GrafPtr theWindow )
{
short wHeight, wWidth, dHeight, dWidth;
GrafPtr wmPort;
if ( theWindow ) /* Make sure that the window is kosher */
{
/* Calculate the window's height and width */
wHeight = theWindow->portRect.bottom - theWindow->portRect.top;
wWidth = theWindow->portRect.right - theWindow->portRect.left;
GetWMgrPort ( &wmPort ); /* Get the primary display window */
/* Calculate it's height and width */
dHeight = wmPort->portRect.bottom - (wmPort->portRect.top + 29);
dWidth = wmPort->portRect.right - wmPort->portRect.left;
/* Center the window in that window */
MoveWindow ( theWindow, ((dWidth - wWidth) / 2) - theWindow->portRect.left,
(((dHeight - wHeight) / 3) - theWindow->portRect.top) + 29, false );
}
}
/************************************************************************************************/
/* Function Name: DisplayWindowCentered */
/* Description: Displays a window centered on the screen */
/* Primary Author: Chris Marshall */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/************************************************************************************************/
/*##############################################################################################*/
/************************************************************************************************/
/* This routine takes a window pointer, and then centers the window on the screen. */
/************************************************************************************************/
void DisplayWindowCentered ( GrafPtr theWindow )
{
if ( theWindow ) /* Make sure that the window is kosher */
{
CenterWindow ( theWindow );
ShowWindow ( theWindow ); /* Show the window */
SelectWindow ( theWindow );
SetPort ( theWindow );
}
}
/************************************************************************************************/
/* Function Name: DrawControlPoint */
/* Description: Draws our little dots */
/* Primary Author: Chris Marshall */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/************************************************************************************************/
/*##############################################################################################*/
/************************************************************************************************/
void DrawControlPoint ( Rect box, short x, short y, Boolean selected )
{
Rect pointRect;
PenState oldPen;
GetPenState ( &oldPen );
PenNormal ( );
PenMode ( patXor );
if ( selected )
{
SetRect ( &pointRect, 0, 0, 4, 4 );
PenSize ( 2, 2 );
OffsetRect ( &pointRect, box.left + (box.left + x) - 2, (box.bottom - y) - 2 );
FrameOval ( &pointRect );
PenSize ( 1, 1 );
SetRect ( &pointRect, 0, 0, 8, 8 );
OffsetRect ( &pointRect, box.left + (box.left + x) - 4, (box.bottom - y) - 4 );
}
else
{
SetRect ( &pointRect, 0, 0, 4, 4 );
PenSize ( 2, 2 );
OffsetRect ( &pointRect, box.left + (box.left + x) - 2, (box.bottom - y) - 2 );
}
FrameOval ( &pointRect );
SetPenState ( &oldPen );
}
/************************************************************************************************/
/* Function Name: DrawSplineLine */
/* Description: Handles drawing of the spline line */
/* Primary Author: Chris Marshall */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/************************************************************************************************/
/*##############################################################################################*/
/************************************************************************************************/
void DrawSplineLine ( Rect box, short selector )
{
RgnHandle oldClip;
register short maxX = box.right - box.left, index;
long x, y;
PenState oldPen;
Pattern pat;
short point;
oldClip = NewRgn ( );
if ( oldClip )
{
RGBColor oldFore, oldBack;
Rect box2;
GetClip ( oldClip );
GetPenState ( &oldPen );
PenNormal ( );
GetForeColor ( &oldFore );
GetBackColor ( &oldBack );
ForeColor ( blackColor );
BackColor ( whiteColor );
ClipRect ( &box );
if ( selector == kDrawNew )
EraseRect ( &box );
PenMode ( patXor );
switch ( selector )
{
case kDrawSelectedPoint:
point = gMySpline->GetSelectedPoint ( );
if ( point )
{
if ( (point != gOldSelectedPoint) && gOldSelectedPoint )
{
x = gOldPoints[gOldSelectedPoint - 1].h;
y = gOldPoints[gOldSelectedPoint - 1].v;
DrawControlPoint ( box, x, y, true );
DrawControlPoint ( box, x, y, false );
}
x = gOldPoints[--point].h;
y = gOldPoints[point].v;
DrawControlPoint ( box, x, y, point == (gOldSelectedPoint - 1) );
DrawControlPoint ( box, x, y, true );
gOldSelectedPoint = point + 1;
}
break;
case kRedrawPoint:
x = gOldPoints[gOldSelectedPoint - 1].h;
y = gOldPoints[gOldSelectedPoint - 1].v;
DrawControlPoint ( box, x, y, true );
if ( gOldSelectedPoint != gMySpline->GetSelectedPoint ( ) )
{
index = gMySpline->GetNumberOfSplinePoints ( );
while ( index )
{
gMySpline->GetSplinePoint ( index--, x, y );
gOldPoints[index].h = x;
gOldPoints[index].v = y;
}
}
gOldSelectedPoint = gMySpline->GetSplinePoint ( x, y );
gOldPoints[gOldSelectedPoint - 1].h = x;
gOldPoints[gOldSelectedPoint - 1].v = y;
DrawControlPoint ( box, x, y, true );
for ( x = 0; x < maxX; x++ )
{
MoveTo ( box.left + x, box.bottom - gOldLine[x] );
Line ( 0, 0 );
gOldLine[x] = y = gMySpline->GetYValue ( x );
MoveTo ( box.left + x, box.bottom - y );
Line ( 0, 0 );
}
break;
case kDrawNew:
EraseRect ( &box );
gOldSelectedPoint = gMySpline->GetSelectedPoint ( );
index = gMySpline->GetNumberOfSplinePoints ( );
while ( index )
{
gMySpline->GetSplinePoint ( index--, x, y );
gOldPoints[index].h = x;
gOldPoints[index].v = y;
DrawControlPoint ( box, x, y, index == (gOldSelectedPoint - 1) );
}
PenSize ( 1, 1 );
for ( x = 0; x < maxX; x++ )
{
gOldLine[x] = y = gMySpline->GetYValue ( x );
MoveTo ( box.left + x, box.bottom - y );
Line ( 0, 0 );
}
break;
}
RGBForeColor ( &oldFore );
RGBBackColor ( &oldBack );
SetPenState ( &oldPen );
SetClip ( oldClip );
DisposeRgn ( oldClip );
}
}
/************************************************************************************************/
/* Function Name: FilterProc */
/* Description: Hacky filter proc - don't do this at home, kids! */
/* Primary Author: Chris Marshall */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/************************************************************************************************/
/*##############################################################################################*/
/************************************************************************************************/
pascal Boolean FilterProc ( DialogPtr theDialog, EventRecord *theEvent, short *theItem )
{
Point where = theEvent->where;
short type;
Handle item;
Rect box;
short numPoints = gMySpline->GetNumberOfSplinePoints ( );
long x, y;
GlobalToLocal ( &where );
GetDItem ( theDialog, 2, &type, &item, &box );
if ( (theEvent->what == keyDown) || (theEvent->what == autoKey) )
switch ( theEvent->message & charCodeMask )
{
case kDeleteKey: // The user can delete spline points
if ( numPoints > 2 )
{
gMySpline->DeleteSplinePoint ( );
DrawSplineLine ( box, kDrawNew );
}
else
SysBeep ( 1 );
*theItem = 2;
return true;
break;
case kTabKey: // The user can tab from point to point
short selectedPoint = gMySpline->GetSelectedPoint ( );
if ( theEvent->modifiers & shiftKey )
{
if ( --selectedPoint < 1 )
selectedPoint = numPoints;
}
else
if ( ++selectedPoint > numPoints )
selectedPoint = 1;
gMySpline->SelectPoint ( selectedPoint );
DrawSplineLine ( box, kDrawSelectedPoint );
*theItem = 2;
return true;
break;
case kLeftArrowKey:
case kRightArrowKey:
case kUpArrowKey:
case kDownArrowKey:
gMySpline->GetSplinePoint ( x, y );
switch ( theEvent->message & charCodeMask )
{
case kLeftArrowKey:
if ( theEvent->modifiers & shiftKey )
x -= 10;
else
x--;
break;
case kRightArrowKey:
if ( theEvent->modifiers & shiftKey )
x += 10;
else
x++;
break;
case kUpArrowKey:
if ( theEvent->modifiers & shiftKey )
y += 10;
else
y++;
break;
case kDownArrowKey:
if ( theEvent->modifiers & shiftKey )
y -= 10;
else
y--;
break;
}
x = MinVal ( (box.right - box.left) - 1, MaxVal ( 0, x ) );
y = MinVal ( (box.bottom - box.top) - 1, MaxVal ( 0, y ) );
gMySpline->ChangeSplinePoint ( x, y );
DrawSplineLine ( box, kRedrawPoint );
*theItem = 2;
return true;
break;
}
if ( (theEvent->what == mouseDown) && (FindDItem ( theDialog, where ) == 1) )
{
Point oldWhere;
short index;
where.h -= box.left;
where.v -= box.top;
where.h = MinVal ( (box.right - box.left) - 1, MaxVal ( 0, where.h ) );
where.v = MinVal ( (box.bottom - box.top) - 1, MaxVal ( 0, where.v ) );
oldWhere = where;
y = ((box.bottom - box.top) - 1) - where.v;
x = where.h;
index = gMySpline->IsASplinePoint ( x, y );
if ( index )
{
gMySpline->SelectPoint ( index );
DrawSplineLine ( box, kDrawSelectedPoint );
}
else
{
index = gMySpline->AddSplinePoint ( where.h, ((box.bottom - box.top) - 1) - where.v );
DrawSplineLine ( box, kDrawNew );
}
while ( StillDown ( ) )
{
GetDItem ( theDialog, 2, &type, &item, &box );
GetMouse ( &where );
where.h -= box.left;
where.v -= box.top;
if ( (where.h < -20) || (where.h > (box.right - box.left) + 20) )
{
if ( (index > 1) && (index < gMySpline->GetNumberOfSplinePoints ( )) )
{
gMySpline->DeleteSplinePoint ( );
DrawSplineLine ( box, kDrawNew );
break;
}
}
where.h = MinVal ( (box.right - box.left) - 1, MaxVal ( 0, where.h ) );
where.v = MinVal ( (box.bottom - box.top) - 1, MaxVal ( 0, where.v ) );
if ( (where.h != oldWhere.h) || (where.v != oldWhere.v) )
{
y = ((box.bottom - box.top) - 1) - where.v;
x = where.h;
index = gMySpline->ChangeSplinePoint ( x, y );
DrawSplineLine ( box, kRedrawPoint );
oldWhere = where;
}
}
*theItem = 2;
return true;
}
else
return false;
}
/************************************************************************************************/
/* Function Name: main */
/* Description: Main context of the test app. */
/* Primary Author: Chris Marshall */
/************************************************************************************************/
/* MODIFICATION HISTORY */
/************************************************************************************************/
/* Date Author Description */
/* */
/* 10/01/93 Chris Marshall I added a little snippet of code to copy the spline points */
/* when switching from fixed point to floating point modes. */
/* */
/************************************************************************************************/
/*##############################################################################################*/
/************************************************************************************************/
void main ( )
{
DialogPtr theDialog;
short type;
Handle item;
Rect box, box2;
short theItem, numPoints;
InitGraf ( &thePort );
InitFonts ( );
InitWindows ( );
InitMenus ( );
TEInit ( );
InitCursor ( );
InitDialogs (0L);
FlushEvents ( everyEvent, 0 );
MaxApplZone ( );
MoreMasters ( );
MoreMasters ( );
MoreMasters ( );
theDialog = GetNewDialog ( 128, 0L, 0L );
if ( theDialog )
{
GetDItem ( theDialog, 3, &type, &item, &box );
SetCtlValue ( (ControlHandle)item, 1 );
GetDItem ( theDialog, 2, &type, &item, &box );
gMySpline = (Spline*)new FixedSpline ( 0, 0, (box.right - box.left) - 1,
(box.bottom - box.top) - 1, kHitRadius );
if ( gMySpline )
{
// We just give enough points for the whole line and say the heck with it
gOldLine = (long*)NewPtr ( sizeof ( long ) * (box.right - box.left) );
gOldPoints = (Point*)NewPtr ( sizeof ( Point ) * (box.right - box.left) );
SetPort ( theDialog );
DisplayWindowCentered ( theDialog );
BackColor ( whiteColor );
ForeColor ( blackColor );
EraseRect ( &box );
InsetRect ( &box, -1, -1 );
FrameRect ( &box );
InsetRect ( &box, 1, 1 );
DrawSplineLine ( box, kDrawNew );
do
{
ModalDialog ( FilterProc, &theItem );
numPoints = gMySpline->GetNumberOfSplinePoints ( );
switch ( theItem ) // Let the user switch classes
{
case 3:
GetDItem ( theDialog, 3, &type, &item, &box2 );
if ( !GetCtlValue ( (ControlHandle)item ) )
{
SetCtlValue ( (ControlHandle)item, 1 );
GetDItem ( theDialog, 4, &type, &item, &box2 );
SetCtlValue ( (ControlHandle)item, 0 );
delete gMySpline;
gMySpline = (Spline*)new FixedSpline ( gOldPoints[0].h, gOldPoints[0].v,
gOldPoints[--numPoints].h,
gOldPoints[numPoints].v, kHitRadius );
while ( numPoints > 1 )
gMySpline->AddSplinePoint ( gOldPoints[--numPoints].h,
gOldPoints[numPoints].v );
DrawSplineLine ( box, kDrawNew );
}
break;
case 4:
GetDItem ( theDialog, 4, &type, &item, &box2 );
if ( !GetCtlValue ( (ControlHandle)item ) )
{
SetCtlValue ( (ControlHandle)item, 1 );
GetDItem ( theDialog, 3, &type, &item, &box2 );
SetCtlValue ( (ControlHandle)item, 0 );
delete gMySpline;
gMySpline = (Spline*)new Spline ( gOldPoints[0].h, gOldPoints[0].v,
gOldPoints[--numPoints].h,
gOldPoints[numPoints].v, kHitRadius );
while ( numPoints > 1 )
gMySpline->AddSplinePoint ( gOldPoints[--numPoints].h,
gOldPoints[numPoints].v );
DrawSplineLine ( box, kDrawNew );
}
break;
}
} while ( theItem != 1 );
DisposPtr ( (Ptr)gOldLine );
DisposPtr ( (Ptr)gOldPoints );
delete gMySpline;
}
DisposDialog ( theDialog );
}
}